Perl 5

      U N L E A S H E D




by Kamran Husain and Robert F. Breedlove




C  O  N  T  E  N  T  S



Introduction

Chapter 1  What is Perl?

Chapter 2  A Brief Introduction to Perl

Chapter 3  References

Chapter 4  Introduction to Perl Modules

Chapter 5  Object-Oriented Programming in Perl

Chapter 6  Binding Variables to Objects

Chapter 7  String and Patterns

Chapter 8  Documenting Perl Scripts

Chapter 9  Portability Issues with Windows and Perl

Chapter 10  Win32 Modules on Windows NT

Chapter 11  Security Issues in Perl

Chapter 12  Using Sockets

Chapter 13  Messaging Facilities: The System V Ipc Functions

Chapter 14  Signals, Pipes, FIFOs, and Perl

Chapter 15  Direct Access to System Facilities

Chapter 16  Command-line Interface with Perl

Chapter 17  GUI Interfaces with Perl/Tk

Chapter 18  Databases for Perl

Chapter 19  Generating Reports

Chapter 20  Introduction to Web Pages and CGI

Chapter 21  Building VRML Models with Perl

Chapter 22  Using HTML FORMs with Perl CGI Scripts

Chapter 23  HTML with Perl Modules

Chapter 24  Building and Installing the Perl 5 Interpreter

Chapter 25  Perl Internal Files and Structures

Chapter 26  Writing C Extensions in Perl

Chapter 27  Writing Extensions in C

Chapter 28  Client/Server Using Java Applets and Perl

Chapter 29  Practical Scripts While Using Perl

Chapter 30  Using the Perl Debugger

Chapter 31  Generating Code

Chapter 32  Dynamic Image Creation

Chapter 33  Debugging CGI Applications

appendix A  Perl 5 Function and Variable Reference

appendix B  Perl Module Archives 737



Credits




This book is dedicated to my parents: Dr. Saleha Bilal Husain and Dr. Bilal Riaz Husain. -KH
To my family, my wife Madeline for giving me the time and encouragement, and my children Delenn, Duncan, and Diego for setting my life in perspective. -BB



Copyright © 1996 by Sams Publishing

FIRST EDITION

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams Publishing, 201 W. 103rd St., Indianapolis, IN 46290.

International Standard Book Number: 0-672-30891-6

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : [email protected]

All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.


PublisherRichard K. Swadley
Publishing Team Leader Greg Wiegand
Managing Editor Cindy Morrow
Director of Marketing John Pierce
Assistant Marketing Managers Kristina Perry, Rachel Wolfe


Acquisitions Editor Christopher Denny Development Editor Anthony Amico
Software Development Specialist Brad Myers Production Editors Kristi Hart, Katherine Ewing
Copy Editor Bart ReedIndexer Chris Barrick
Technical Reviewers Zachary Bir, John Fleming, Elliotte Harold, Scott Hutton, Matt Liggett, John Neinart Editorial Coordinator Bill Whitmer
Technical Edit Coordinator Lynette Quinn Editorial Assistants Carol Ackerman, Andi Richter, Rhonda Tinch-Mize
Cover Designer Tim AmrheinBook Designer Gary Adair
Copy Writer Peter FullerProduction Team Supervisor Brad Chinn
ProductionDebra Bolhuis, Mona Brown, Michael Dietsch, Jason Hand, Daniel Harris, Bill Hartman, Susan Knose, Clint Lahnen, Paula Lowell, Laura Robbins, Bobbi Satterfield, Susan Van Ness, Suzanne Whitmer


Acknowledgements

First of all, I would like to thank Chris Denny at Sams for giving me the opportunity to write a book on Perl 5. I would also to thank Kristi Hart, Tony Amico, and Bart Reed for their patience, help, advice, and forcing me to keep up with deadlines.

I'd like to thank Uzma, my wife, and Haya and Hana, my twin daughters, for putting up with my weird schedules and odd hours.

Last, but definitely not the least, thanks to all the Perl programmers on Perl mailing lists for all the critique and comments via e-mail.

Kamran Husain
1996

About the Authors

Kamran Husain is a software consultant specializing in developing real-time applications, data acquisition, and turn-key systems in all sorts of software environments including C++, Java, Windows NT and 95, JavaScript, and Motif. He can be reached via e-mail at [email protected].

Robert F. Breedlove is a senior systems engineer with EDS. He has over 20 years of experience in data processing including extensive client/server, UNIX, intranet and Internet experience. He can be reached at [email protected], or at his homepage: http://www.channel1.com/users/rbreed01/.

Tell Us What You Think!

As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.

Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.

Note
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668.

As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:

FAX:317/581-4669
E-mail:[email protected]
Mail:Greg Wiegand
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290


Introduction

This book documents a very powerful language called Perl 5, which is version 5 of Larry Wall's creation, Perl. Perl is fast becoming the de facto language for UNIX system administrators, Webmasters on the World Wide Web, and programmers who want a fast, powerful, and easy-to-use program language. This book will provide you with the basics of the language and introduce you to the tools available for Perl.

While writing this book, I assumed that you, the reader, have had some prior programming experience. If you do not have any prior programming experience, I strongly suggest not skipping the first three chapters. If you are already a programmer and are familiar with Perl 4, this book should provide you with enough knowledge to use the great new features.

How This Book Is Organized

This book is divided into six parts.

Part I: The Basics

The first part provides a brief introduction to Perl as a programming language. It covers the use of references to variables, regular expressions, and the fundamentals of programming in Perl by using modules. It introduces the use of Perl for programming with an object-oriented paradigm. It also covers the not-so-basic but very important topic of tying variables in Perl programs.

Part II: Communications

The second part covers applying Perl to different platforms and types of applications. It begins with a chapter on processing patterns and strings in Perl. For a long time, I debated reversing the order of Chapters 6 and 7 because the concepts are both fundamental, but the chapter on tying variables seemed to fit in the first part. Most of the portability issues in programming in Perl on a Windows platform are also discussed in this part.

Part III: Presenting and Sharing Data

The third part gets into networking and system-level programming issues for Perl. The basic communications features in Perl include sockets, System V Ipc messaging facilities, signals, pipes, and FIFOs, and are covered in this part. The basics of handling the command-line interface as well as a graphical user interface using Tcl/Tk and Perl are also covered in this part. The last two chapters of this part introduce the use of database front-ends and the report-generating facilities of Perl. The information in this part serves as the basis for writing Perl applications that can provide reports on data stored in databases.

Part IV: Working with the Web

Section four introduces the World Wide Web and the Common Gateway Interface (CGI). Thick books have been written on these topics, and this part attempts to present the most practical information required to get you started writing CGI scripts and your own Web pages. This part covers how to access databases from within Perl and how to write your own module to generate VRML objects. The information in this chapter can serve as a basis for writing your own CGI scripts using the modules available on the World Wide Web for Perl.

Part V: Under the Hood

The chapters in this part are geared towards the advanced user or the system administrator who has to install Perl on various platforms. The topics in this part tend to be centered on UNIX systems, but that's just where most of the heavy Perl development seems to be. I cover the internal data types used in Perl, and then based on this knowledge I go on to show the techniques of writing extensions and embedding Perl in C programs.

This part covers the use of the Perl debugger. This was the part where I wanted to present some case studies on how to use Perl as a prototyping language to solve problems. I use Perl to solve some real-world problems involving parsing weird data formats and creating images on demand, and then I discuss issues on how to debug CGI applications. This part also covers how to use Java applets on a client to communicate with Perl servers, which is a hot Internet topic.

Conventions Used in This Book

The following typographic conventions are used in this book:

There's Always a Better Way

Now for a personal disclaimer of sorts. Even though I attempt to cover all the major points of the latest release of Perl, version 5.002 at the time we go to print, I probably will never be able to list all the features of this wonderful language. If there is one thing that I've learned while working with Perl, it is that there are so many ways of doing any one task. Every time I write a Perl script, I think that I "could've done it this way and maybe it'll run faster." The number of combinations of things to do with Perl never really ends. So, if you find a better way of writing Perl scripts than the ones shown in this book, don't be surprised-it's bound to happen. Do drop me a line, though, because I will incorporate them into the next edition of this book. I look forward to receiving all your comments and, yes, even criticisms and the usual flames.


Sincerely,
Kamran Husain